<template>
    <section>
        <!--工具条-->
        <el-col :span="24" class="toolbar">
            <el-form :inline="true" :model="formInline" class="demo-form-inline">
                <el-form-item>
                    <el-input v-model="formInline.keyword" placeholder="关键字"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button @click="init">查询</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button @click="handleAdd">新增</el-button>
                </el-form-item>
            </el-form>
        </el-col>
        <!-- 用户列表 -->
        <el-table :data="tableData" highlight-current-row v-loading="listLoading" style="width: 100%;">
            <el-table-column type="index" width="50">
            </el-table-column>
            <el-table-column prop="name" label="权限">
            </el-table-column>
            <el-table-column prop="info" label="说明">
            </el-table-column>
            <el-table-column prop="projectName" label="项目">
            </el-table-column>
            <el-table-column inline-template label="状态">
                <span>
                    <el-switch v-model=row.type @change="editStatus(row)" on-text="启用" off-text="停用" on-color="#13ce66" off-color="#ff4949">
                    </el-switch>
                </span>
            </el-table-column>
            <el-table-column inline-template :context="_self" label="操作">
                <span>
                    <el-button type="text" size="small" @click="handleEdit(row)">编辑</el-button>
                    <el-button type="text" size="small" @click="handleDel(row.priId)">删除</el-button>
                </span>
            </el-table-column>
        </el-table>
        <!--编辑界面-->
        <el-dialog :title="editFormTtile" v-model="editFormVisible" :close-on-click-modal="false">
            <el-form :model="editForm" label-width="100px" :rules="editFormRules" ref="editForm">
                <el-form-item label="权限" prop="name">
                    <el-input auto-complete="off" v-model="editForm.name"></el-input>
                </el-form-item>
                <el-form-item label="项目" prop="pId">
                    <el-select @change="getApis" v-model="editForm.pId" placeholder="请选择" :disabled="editForm.projectDisabled">
                        <el-option v-for="item in projects" :key="item.id" :label="item.name" :value="item.pId">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="说明" prop="info">
                    <el-input auto-complete="off" v-model="editForm.info"></el-input>
                </el-form-item>
                <el-form-item label="接口" prop='apiList'>
                    <el-select v-model="editForm.apiList" multiple placeholder="请选择">
                        <el-option v-for="item in apis" :key="item.id" :label="item.name" :value="item.apiId">
                        </el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="editFormVisible = false">取 消</el-button>
                <el-button type="primary" @click.native="editSubmit('editForm')" :loading="editLoading">{{btnEditText}}</el-button>
            </div>
        </el-dialog>
    </section>
</template>
<script>
import util from 'common/js/util'
import NProgress from 'nprogress'
import * as AuthorityAction from './AuthorityAction'

export default {

    data() {
            return {
                formInline: {
                    keyword: ''
                },
                value: true,
                editFormVisible: false, //编辑界面显是否显示
                editFormTtile: '编辑', //编辑界面标题
                //编辑界面数据
                editForm: {
                    priId: '',
                    name: '',
                    info: '',
                    pId: '',
                    apiList: [],
                    projectDisabled: false,
                },
                editLoading: false,
                btnEditText: '提 交',
                editFormRules: {
                    name: [{
                        required: true,
                        message: '请填写权限',
                        trigger: 'blur'
                    }, ],
                    pId: [{
                        required: true,
                        message: '请选择项目',
                        trigger: 'change'
                    }, ],
                    info: [{
                        required: true,
                        message: '请填写说明',
                        trigger: 'blur'
                    }, ],
                    apiList: [{
                        required: true,
                        message: '请选择接口',
                        trigger: 'change',
                        type: 'array'
                    }, ]
                },
                tableData: [],
                listLoading: false,
                projects: [],
                addAuthorityForm: {
                    projects: []
                },
                apis: []
            }
        }, created: function() {
            this.init()
        },
        methods: {
            //状态显示转换  :formatter="formatType" 加上这个可以处理程序 转换
            formatType: function(row, column) {
                return row.type == 1 ? true : row.type == 0 ? false : true;
            },
            //删除记录
            handleDel: function(priId) {
                //console.log(row);
                var _this = this;
                this.$confirm('确认删除该记录吗?', '提示', {
                    //type: 'warning'
                }).then(() => {
                    _this.listLoading = true;
                    NProgress.start();
                    AuthorityAction.delAuthority(priId).then(function(response) {
                        _this.listLoading = false;
                        NProgress.done();
                        if (response.ret == 200) {
                            _this.$notify({
                                title: '成功',
                                message: '删除成功',
                                type: 'success'
                            });
                            _this.init()
                        } else {
                            _this.$message.error(response.msg);
                        }
                    })
                }).catch(() => {

                });
            },
            //显示编辑界面
            handleEdit: function(row) {
                if (this.$refs["editForm"]) {
                    this.resetForm("editForm")
                }
                var apiList = new Array()
                for (var i in row.apiList) {
                    apiList.push(row.apiList[i].apiId)
                }
                this.editForm.projectDisabled = true
                this.editForm.apiList = apiList
                this.editFormVisible = true;
                this.editFormTtile = '编辑权限'
                this.editForm.name = row.name
                this.editForm.info = row.info
                this.editForm.pId = row.pId
                this.editForm.priId = row.priId
                var _this = this
                AuthorityAction.GetApisByPId(row.pId).then(function(response) {
                    _this.apis = response.data
                })
            },
            //编辑 or 新增
            editSubmit: function(formName) {
                var _this = this
                _this.$refs[formName].validate((valid) => {
                    if (valid) {

                        _this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            _this.editLoading = true;
                            NProgress.start();
                            _this.btnEditText = '提交中';
                            var params = _this.editForm
                            if (params.priId) {
                                //修改
                                var rs = AuthorityAction.updateAuthority(params.priId, params.name, params.info, params.pId, JSON.stringify(params.apiList))
                                rs.then(function(response) {
                                    _this.btnEditText = '提 交';
                                    _this.editLoading = false;
                                    NProgress.done();
                                    if (response.ret == '200') {
                                        _this.$notify({
                                            title: '成功',
                                            message: '修改成功',
                                            type: 'success'
                                        });
                                        _this.init()
                                        _this.editFormVisible = false;
                                    } else {
                                        _this.$message.error(response.msg);
                                    }
                                    _this.listLoading = false;
                                })
                            } else {
                                //增加
                                var rs = AuthorityAction.addAuthority(params.name, params.info, params.pId, JSON.stringify(params.apiList))
                                rs.then(function(response) {
                                    _this.btnEditText = '提 交';
                                    _this.editLoading = false;
                                    NProgress.done();
                                    if (response.ret == '200') {
                                        _this.$notify({
                                            title: '成功',
                                            message: '增加成功',
                                            type: 'success'
                                        });
                                        _this.editFormVisible = false;
                                        _this.init()
                                    } else {
                                        _this.$message.error(response.msg);
                                    }
                                    _this.listLoading = false;
                                })
                            }
                        });
                    } else {
                        alert('请填写必填项')
                        return false;
                    }
                })
            },
            //显示新增界面
            handleAdd: function() {
                if (this.$refs["editForm"]) {
                    this.resetForm("editForm")
                    this.editForm.name = ''
                    this.editForm.info = ''
                    this.editForm.pId = ''
                }
                this.editForm.projectDisabled = false
                this.editForm.apiList = []
                this.editFormVisible = true
                this.editFormTtile = '新增权限'
            },
            getApis() {
                var _this = this;
                var pId = _this.editForm.pId;
                var rs = AuthorityAction.GetApisByPId(pId)
                rs.then(function(response) {
                    _this.apis = response.data
                })
            },
            resetForm(formName) {
                this.$refs[formName].resetFields()
            },
            init() {
                var _this = this
                var keyword = _this.formInline.keyword
                var rs = AuthorityAction.GetAuthorityList(keyword)
                rs[0].then(function(response) {
                    if (response.ret == 200) {
                        _this.tableData = response.data
                    }
                })
                rs[1].then(function(response) {
                    if (response.ret == 200) {
                        _this.projects = response.data
                    }
                })
            },
            editStatus(row) {
                var _this = this
                _this.listLoading = true;
                var type = row.type ? 1 : 0;
                var rs = AuthorityAction.editStatus(row.priId, type);
                rs.then(function(response) {
                    if (response.ret == 200) {
                        var typeDescribe = row.type == true ? "启用成功" : "停用成功";
                        _this.$notify({
                            title: '成功',
                            message: typeDescribe,
                            type: 'success',
                            duration: 1000
                        });
                    } else {
                        _this.$notify({
                            title: '异常',
                            message: response.msg,
                            type: 'error',
                        });
                        row.type = !row.type
                    }
                    _this.listLoading = false;
                })
            },
        }
}
</script>
<style scoped>
@import "Authority.css";
</style>
